<?php

use think\facade\Route;

// 跨域处理
Route::options('*', function () {
    return response('', 200);
});

// 公开接口（无需认证）
Route::group('api', function () {

    // 认证相关
    Route::group('auth', function () {
        Route::post('register', 'Auth/register');
        Route::post('login', 'Auth/login');
        Route::post('wechat-login', 'Auth/wechatLogin');
        Route::post('alipay-login', 'Auth/alipayLogin');
        Route::post('douyin-login', 'Auth/douyinLogin');
        Route::post('refresh-token', 'Auth/refreshToken');
    });

    // 系统接口
    Route::group('system', function () {
        Route::get('icons', 'System/getIcons');
        Route::get('encrypt-methods', 'System/getEncryptMethods');
        Route::get('public-config', 'System/getPublicConfig');
        Route::get('version', 'System/checkVersion');
        Route::get('app-info', 'System/getAppInfo');
        Route::get('health', 'System/health');
        Route::post('feedback', 'System/feedback');
    });

    // 分类模板接口
    Route::get('categories/templates', 'Category/templates');

})->middleware([
    \app\middleware\Cors::class,
    \app\middleware\RateLimit::class,
    \app\middleware\Log::class,
]);

// 需要认证的接口
Route::group('api', function () {

    // 认证相关（需要登录）
    Route::group('auth', function () {
        Route::post('bind', 'Auth/bindOauth');                      // 绑定第三方账号
        Route::delete('unbind', 'Auth/unbindOauth');                // 解绑第三方账号
        Route::get('bindings', 'Auth/getBindings');                 // 获取绑定列表
        Route::post('logout', 'Auth/logout');                       // 退出登录
    });

    // 用户相关
    Route::group('user', function () {
        Route::get('info', 'User/getInfo');
        Route::put('info', 'User/updateInfo');
        Route::put('password', 'User/changePassword');
        Route::put('change-master-password', 'User/changeMasterPassword');
        Route::put('encrypt-method', 'User/changeEncryptMethod');
        Route::get('devices', 'User/getDevices');
        Route::delete('devices', 'User/deleteDevice');
        Route::get('logs', 'User/getLogs');
        Route::post('avatar', 'User/uploadAvatar');
        Route::post('verify-email', 'User/sendEmailVerification');
        Route::post('verify-phone', 'User/sendPhoneVerification');
        Route::put('verify-email', 'User/verifyEmail');
        Route::put('verify-phone', 'User/verifyPhone');
        Route::delete('account', 'User/deleteAccount');
    });

    // 分类管理
    Route::group('categories', function () {
        Route::get('', 'Category/index');
        Route::post('', 'Category/save');
        Route::put(':id', 'Category/update');
        Route::delete(':id', 'Category/delete');
        Route::get('stats', 'Category/stats');
    });

    // 账号管理
    Route::group('accounts', function () {
        Route::get('stats', 'Account/stats');
        Route::get('favorites', 'Account/favorites');
        Route::get('search', 'Account/search');
        Route::get('expiring', 'Account/expiring');
        Route::get('expired', 'Account/expired');
        Route::post('batch-delete', 'Account/batchDelete');
        Route::post('batch-update-category', 'Account/batchUpdateCategory');
        Route::get('', 'Account/index');
        Route::post('', 'Account/save');
        Route::get(':id', 'Account/read');
        Route::post(':id/decrypt', 'Account/readDecrypted');
        Route::put(':id', 'Account/update');
        Route::delete(':id', 'Account/delete');
        Route::post(':id/copy-password', 'Account/copyPassword');
        Route::post(':id/toggle-favorite', 'Account/toggleFavorite');
    });

    // 导入导出
    Route::group('import', function () {
        Route::get('export-json', 'Import/exportJson');
        Route::get('export-csv', 'Import/exportCsv');
        Route::get('list-backups', 'Import/listBackups');
        Route::post('import-json', 'Import/importJson');
        Route::post('import-csv', 'Import/importCsv');
        Route::post('create-backup', 'Import/createBackup');
        Route::post('restore-backup', 'Import/restoreBackup');
        Route::delete('delete-backup', 'Import/deleteBackup');
    });

    // 通知
    Route::group('notifications', function () {
        Route::get('', 'Notification/index');
        Route::get('stats', 'Notification/stats');
        Route::get('expiring', 'Notification/expiring');
        Route::get('expired', 'Notification/expired');
        Route::post('batch-set-expiration', 'Notification/batchSetExpiration');
        Route::post('set-default-expiration', 'Notification/setDefaultExpiration');
    });

    // 统计
    Route::group('statistics', function () {
        Route::get('', 'Statistics/index');
        Route::get('account-usage', 'Statistics/accountUsage');
        Route::get('most-used', 'Statistics/mostUsed');
        Route::get('trend', 'Statistics/trend');
        Route::get('category-usage', 'Statistics/categoryUsage');
        Route::get('action-types', 'Statistics/actionTypes');
        Route::get('recent-activities', 'Statistics/recentActivities');
        Route::get('health-score', 'Statistics/healthScore');
    });

})->middleware([
    \app\middleware\Cors::class,
    \app\middleware\Auth::class,
    \app\middleware\RateLimit::class,
    \app\middleware\Log::class,
]);

// 异常处理
Route::miss(function () {
    return json([
        'code' => 404,
        'message' => '接口不存在',
        'data' => null,
        'timestamp' => time()
    ])->code(404);
});